package com.it.ems.mapper;

import com.it.ems.entity.Address;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

@Mapper
public interface AddressMapper {
    @Select("SELECT id, user_id as userId, address, COALESCE(is_default, false) as isDefault, created_at as createdAt, updated_at as updatedAt FROM addresses WHERE id = #{id}")
    Address findById(Long id);
    
    @Select("SELECT id, user_id as userId, address, COALESCE(is_default, false) as isDefault, created_at as createdAt, updated_at as updatedAt FROM addresses WHERE user_id = #{userId}")
    List<Address> findByUserId(Long userId);
    
    @Insert("INSERT INTO addresses (user_id, address, is_default, created_at, updated_at) " +
            "VALUES (#{userId}, #{address}, COALESCE(#{isDefault}, false), NOW(), NOW())")
    int insert(Address address);
    
    @Update("UPDATE addresses SET address = #{address}, is_default = COALESCE(#{isDefault}, false), updated_at = NOW() " +
            "WHERE id = #{id}")
    int update(Address address);
    
    @Delete("DELETE FROM addresses WHERE id = #{id}")
    int delete(Long id);
    
    @Delete("DELETE FROM addresses WHERE user_id = #{userId}")
    int deleteByUserId(Long userId);
} 